/** 获取随机歌曲内容 */
function getRandomSong(keyword=''){
	var params = {
		'keyword':keyword,
	};
	var songs = false;
	$.ajax({
        url:'https://api.bbbug.com/api/song/search',
        data:params,
        type:'post',
        dataType:'json',
        async:false,
        success:function (response) {
        	if(response.code=='200'){
        		songs = response.data;
        	}
            console.log('歌曲列表获取成功');
            console.log(response);
        },
        error:function (response) {
            console.log('歌曲列表获取失败');
            console.log(response);
        }
    });
    return songs;
}

/** 获取url参数 */
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}


/** 关键字校验功能 */
function checkKeyword(keyword){
    if(keyword.indexOf('清除歌单')!=-1){
       player.list.clear();
    }else if(keyword.indexOf('下一首')!=-1){
        player.skipForward();
        player.play();
    }else if(keyword.indexOf('上一首')!=-1){
        player.skipBack();
        player.play();
    }else if(keyword.indexOf('我想听')!=-1){
        keyword = keyword.substr(keyword.indexOf('我想听') + 3);
        if(song = getRandomSong(keyword)){
         
            if($('input[name=autoPlay]:checked').length==0){
                var html =  '<li class="item"  data-name="'+song[0].name+'" data-artist="'+song[0].singer+'" data-url="https://api.bbbug.com/api/song/playurl?mid='+song[0].mid+'" data-cover="'+song[0].pic+'">'+
                            '<span class="title">'+song[0].name+'</span>'+
                               ' <span class="action">' + 
                                    '<span class="approval">通过</span>' + 
                                    '<span class="reject">拒绝</span>' + 
                                '</span>'+ 
                            '</li>'
                $('#primary').append(html);

                // $('#primary').scrollTop($('#primary')[0].scrollHeight - $('#primary')[0].clientHeight);


            }else{
                player.list.add([{
                    name: song[0].name,
                    artist: song[0].singer,
                    url: 'https://api.bbbug.com/api/song/playurl?mid='+song[0].mid,
                    cover: song[0].pic
                }]);
                player.list.switch(player.list.audios.length-1);
                player.pause();
                player.play();
            }

            console.log('点歌成功');
        }else{
            console.log('歌曲检索失败');
        }
    }
}

/** 获取时间戳 */
function getTimestamp(offset = 0){
    return Number(Date.parse(new Date())/1000) + Number(offset);
}

